package com.yjps.system.service;

import com.yjps.common.page.Pager;
import com.yjps.system.dao.CodeDao;
import com.yjps.system.model.Code;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jessy on 2015/8/22.
 */
@Service
public class CodeService {
    Logger log = LoggerFactory.getLogger(CodeService.class);

    @Autowired
    CodeDao codeDao;
    public Code findCode(int id) {
        return codeDao.load(id);
    }

    public List<Code> codeList() {
        String hql = "from Code c where c.isValid = 1";
        return codeDao.find(hql);
    }



    @Transactional
    public void addCode(Code code) {
        codeDao.save(code);
    }

    @Transactional
    public Code updateCode(Code code) {
        String hql = "update Code c set c.codeKey =:codeKey,c.codeValue =:codeValue,c.codeDescription =:codeDescription,c.codeState =:codeState where c.id =:id";

        Map<String, Object> map = new HashMap<>();
        map.put("codeKey",code.getCodeKey());
        map.put("codeValue",code.getCodeValue());
        map.put("codeDescription",code.getCodeDescription());
        map.put("codeState",code.getCodeState());
        map.put("id",code.getId());
        codeDao.excute(hql, map);
        return codeDao.load(code.getId());
    }

    @Transactional
    public void deleteCode(int id) {
        codeDao.invalid(findCode(id));
    }
    public Code findCodeByKey(int codetypeId,String key) {
        String hql = "from Code c where c.codeKey =:codeKey and c.codetypeId =:codetypeId";
        Map<String, Object> map = new HashMap<>();
        map.put("codeKey", key);
        map.put("codetypeId", codetypeId);
        return codeDao.findUniqueResult(hql, map);
    }

    public Pager<Code> getUserByPager(Pager<Code> pager) throws Exception {
        Map<String, Object> params = new HashMap<>();
        String queryString = "from Code t where t.isValid = 1";
        queryString += " order by t.id desc";
        List<Code> accounts = codeDao.findWithSelect(queryString, params, (pager.getPageNo()-1)*pager.getPageSize(), pager.getPageSize());
        pager.setCount(codeDao.getTotalCount(queryString, params));
        pager.setList(accounts);
        return pager;
    }
    public Code getCodeByTypeAndKey(int codetypeId,String key){
        String hql = "from Code c where c.codetypeId =:codetypeId and c.codeKey =:codeKey";
        Map<String,Object> params = new HashMap<>();
        params.put("codetypeId", codetypeId);
        params.put("codeKey", key);
        return codeDao.findUniqueResult(hql,params);
    }

    public List<Code> getCodeByType(int codetypeId){
        String hql = "from Code c where c.codetypeId =:codetypeId and c.isValid = 1";
        Map<String,Object> params = new HashMap<>();
        params.put("codetypeId", codetypeId);
        return codeDao.find(hql,params);
    }

}
